package com.sugon.controller.device;

import com.sugon.annotation.AutoLog;
import com.sugon.entity.Follow8dRecordEntity;
import com.sugon.service.Follow8dRecordService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 8D跟踪反馈记录Controller
 *
 * @author sugon
 * @date 2019-08-14 15:46:07
 */
@Controller
@RequestMapping("follow8drecord")
public class Follow8dRecordController {
    @Autowired
    private Follow8dRecordService follow8dRecordService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("follow8drecord:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<Follow8dRecordEntity> follow8dRecordList = follow8dRecordService.queryList(query);
        int total = follow8dRecordService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(follow8dRecordList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("follow8drecord:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        Follow8dRecordEntity follow8dRecord = follow8dRecordService.queryObject(id);

        return R.ok().put("follow8dRecord", follow8dRecord);
    }

    /**
     * 保存
     */
    @AutoLog("8D跟踪反馈记录--保存")
    @RequestMapping("/save")
    @RequiresPermissions("follow8drecord:save")
    @ResponseBody
    public R save(@RequestBody Follow8dRecordEntity follow8dRecord) {
        follow8dRecordService.save(follow8dRecord);

        return R.ok();
    }

    /**
     * 修改
     */
    @AutoLog("8D跟踪反馈记录--修改")
    @RequestMapping("/update")
    @RequiresPermissions("follow8drecord:update")
    @ResponseBody
    public R update(@RequestBody Follow8dRecordEntity follow8dRecord) {
        follow8dRecordService.update(follow8dRecord);

        return R.ok();
    }

    /**
     * 删除
     */
    @AutoLog("8D跟踪反馈记录--删除")
    @RequestMapping("/delete")
    @RequiresPermissions("follow8drecord:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        follow8dRecordService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<Follow8dRecordEntity> list = follow8dRecordService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 审核
     */
    @AutoLog("8D跟踪反馈记录--审核")
    @RequestMapping("/review")
    @RequiresPermissions("follow8drecord:review")
    @ResponseBody
    public R review(@RequestBody Follow8dRecordEntity follow8dRecord) {
        Follow8dRecordEntity follow8dRecordInDB = follow8dRecordService.queryObject(follow8dRecord.getId());
        if (follow8dRecordInDB == null) {
            return R.error("8D跟踪反馈记录不存在！");
        }
        return follow8dRecordService.review(follow8dRecord) > 0 ? R.ok() : R.error();
    }

    /**
     * 复审
     */
    @AutoLog("8D跟踪反馈记录--复审")
    @RequestMapping("/recheck")
    @RequiresPermissions("follow8drecord:recheck")
    @ResponseBody
    public R recheck(@RequestBody Follow8dRecordEntity follow8dRecord) {
        Follow8dRecordEntity follow8dRecordInDB = follow8dRecordService.queryObject(follow8dRecord.getId());
        if (follow8dRecordInDB == null) {
            return R.error("8D跟踪反馈记录不存在！");
        }
        return follow8dRecordService.recheck(follow8dRecord) > 0 ? R.ok() : R.error();
    }

    @AutoLog("8D跟踪反馈记录--维修工单处理时保存")
    @RequestMapping("/save8D")
    @ResponseBody
    public R save8D(@RequestBody Follow8dRecordEntity follow8dRecord) {
        int i = follow8dRecordService.saveDev8D(follow8dRecord);
        return R.ok().put("i", i);
    }
}
